/**
 * Created by LiuLuKang on 2017/9/27.
 */
var mycanvas = document.getElementById('mycanvas').getContext('2d');
//var bgImg = document.getElementById('background');
var fish2 = document.getElementById('fish2');
var fish3 = document.getElementById('fish3');
var fish1 = document.getElementById('fish1');

function Fish(mycanvas, imgNode, cutImgX, cutImgY, cutWidth, cutHeight,cutImgCount) {
	this.mycanvas = mycanvas;
	this.X = Math.random() * 100;//鱼的横坐标
	this.Y = parseInt(Math.random()*350);//鱼的纵坐标
	this.imgNode = imgNode;
	this.cutImgX = cutImgX;//剪切图片的起始X坐标
	this.cutImgY = cutImgY;//剪切图片的起始y坐标
	this.cutWidth = cutWidth;
	this.cutHeight = cutHeight;
	this.cutImgCount=cutImgCount;
	this.speed = Math.random()*6+2;//速度
//		this.cutImgCount=cutImgCount;//剪切图片的数量
	this.move = function () {
		this.X += this.speed;
		this.cutImgY += cutHeight;
		if (this.cutImgY > this.cutHeight*(cutImgCount-1)) {
			this.cutImgY = 0;
		}
		this.drawFish();
	};
	this.drawFish = function () {

		this.mycanvas.beginPath();
		this.mycanvas.drawImage(this.imgNode,
			this.cutImgX, this.cutImgY,
			this.cutWidth, this.cutHeight, this.X, this.Y, this.cutWidth, this.cutHeight);
		this.mycanvas.stroke();
	};
	this.drawFish();
}
var fishArray = [];
function creatFish() {
	if(parseInt(Math.random()*2)==1){
		var fish = new Fish(mycanvas, fish2, 0, 0, 77, 59,4);
		fishArray.push(fish);
	}else if(parseInt(Math.random()*2)==0){
		var fish = new Fish(mycanvas, fish3, 0, 0, 72, 56,4);
	}else {
		var fish = new Fish(mycanvas, fish1, 0, 0, 174, 126,12);
	}
	fishArray.push(fish);
}
function moveFish() {
	for (var i = 0; i < fishArray.length; i++) {
		fishArray[i].move();
	}
}

var creatspeed = 3000;
var newTime = 100;
function startgame() {
	newTime += 100;
	this.mycanvas.clearRect(0, 0, 1200, 750);
	if (newTime % creatspeed == 0) {
		creatFish();
	}
	moveFish();
}
creatFish();

setInterval(startgame, 100);
